import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/application/smart_warehouse/product_storage_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/smart_warehouse/product_management/product_details/product_details_controller.dart';

///产品详情页面
class ProductDetailsPage extends StatelessWidget {
  ProductDetailsPage({super.key});

  final controller = Get.put(ProductDetailsController());

  /// 获取产品基础信息视图
  Widget _getBasicInformation() {
    return Container(
      height: 140.w,
      width: AppDimens.sh,
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.w),
      color: const Color(0xFF5C6DFF),
      child: Column(
        children: [
          Row(
            children: [
              SvgPicture.asset(
                AppImages.icon_title_product,
                width: 17.w,
                height: 17.w,
              ),
              AppGaps.hGap6,
              Expanded(
                  child: Text(
                '产品名称:${controller.productEntity.name}',
                style: TextStyle(
                  fontSize: 16.sp,
                  color: Colors.white,
                  overflow: TextOverflow.ellipsis,
                  fontWeight: FontWeight.w500,
                ),
              )),
              Container(
                padding: EdgeInsets.symmetric(horizontal: 9.w, vertical: 6.w),
                decoration: BoxDecoration(
                  color: const Color(0x1Fffffff),
                  borderRadius: BorderRadius.circular(14.w),
                ),
                child: Text(
                  '出库记录',
                  style: TextStyle(
                    overflow: TextOverflow.ellipsis,
                    fontSize: 13.sp,
                    color: Colors.white,
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ).clickOnce(controller.toOutboundRecordsPage)
            ],
          ),
          AppGaps.vGap8,
          Row(
            children: [
              Expanded(
                child: Row(
                  children: [
                    Text(
                      '产品级别：',
                      style: TextStyle(
                        overflow: TextOverflow.ellipsis,
                        fontSize: 13.sp,
                        color: Colors.white,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                    Expanded(
                        child: Text(
                      '${controller.productEntity.productLevelName}',
                      style: TextStyle(
                        overflow: TextOverflow.ellipsis,
                        fontSize: 12.sp,
                        color: Colors.white,
                        fontWeight: FontWeight.w400,
                      ),
                    )),
                    Text(
                      '产品类别：',
                      style: TextStyle(
                        overflow: TextOverflow.ellipsis,
                        fontSize: 13.sp,
                        color: Colors.white,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                    Expanded(
                        child: Text(
                      '${controller.productEntity.productCategoryName}',
                      style: TextStyle(
                        overflow: TextOverflow.ellipsis,
                        fontSize: 12.sp,
                        color: Colors.white,
                        fontWeight: FontWeight.w400,
                      ),
                    ))
                  ],
                ),
              ),
              Container(
                height: 25.w,
                width: 2.w,
                margin: EdgeInsets.only(right: 20.w, left: 10.w),
                decoration: const BoxDecoration(
                    gradient:
                        LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
                  Color(0x00ffffff),
                  Color(0xffffffff),
                  Color(0x00ffffff),
                ])),
              ),
              _getExpandButton()
            ],
          )
        ],
      ),
    );
  }

  /// 获取展开按钮
  Widget _getExpandButton() {
    return Row(
      children: [
        Text(
          controller.isExpanded ? '收起' : '展开',
          style: TextStyle(
            fontSize: 14.sp,
            color: Colors.white,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.hGap6,
        SvgPicture.asset(
          controller.isExpanded ? AppImages.icon_circle_up : AppImages.icon_circle_down,
          width: 16.w,
          height: 16.w,
        )
      ],
    ).clickOnce(controller.onExpanded);
  }

  /// 获取产品详情视图
  Widget _getDetailsView() {
    return Container(
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 20.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(
          Radius.circular(6.w),
        ),
      ),
      child: Column(
        children: [
          Row(
            children: [
              _getFieldView('组合类型：', '${controller.productEntity.combinationTypeName}'),
              _getFieldView('组合发数：', '${controller.productEntity.combinationQuantity}'),
            ],
          ),
          AppGaps.vGap10,
          Row(
            children: [
              _getFieldView('箱含量：', '${controller.productEntity.boxContent}'),
              _getFieldView('单筒药量：', '${controller.productEntity.dosage}g'),
            ],
          ),
          AppGaps.vGap10,
          Row(
            children: [
              _getFieldView('总药量：', '${controller.productEntity.totalDosage}'),
              _getFieldView('毛重：', '${controller.productEntity.netWeight}'),
            ],
          ),
          Visibility(
              visible: controller.isExpanded,
              child: Column(
                children: [
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('体积：', '${controller.productEntity.volume}'),
                      _getFieldView('燃放效果：', '${controller.productEntity.firingEffectsName}'),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(children: [
                    _getFieldView('然放者最低年龄要求：', '${controller.productEntity.minimumAgeRequirement}岁')
                  ]),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('燃放安全距离：', '${controller.productEntity.safeDistanceName}'),
                      _getFieldView('供应商：', '${controller.productEntity.productSupplier}'),
                    ],
                  ),
                ],
              ))
        ],
      ),
    );
  }

  /// 字段显示
  Widget _getFieldView(String title, String value) {
    return Expanded(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title,
            style: TextStyle(
              fontSize: 13.sp,
              height: 1.2,
              color: const Color(0xFFA5A6B5),
              fontWeight: FontWeight.w400,
            ),
          ),
          Expanded(
              child: Text(
            value,
            style: TextStyle(
              fontSize: 13.sp,
              height: 1.2,
              color: const Color(0xFF495061),
              fontWeight: FontWeight.w400,
            ),
          ))
        ],
      ),
    );
  }

  /// 获取产品储存量视图
  Widget _getStorageCapacityView() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 20.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(
          Radius.circular(6.w),
        ),
      ),
      child: Column(
        children: [
          Row(
            children: [
              SvgPicture.asset(
                AppImages.icon_title,
                width: 20.w,
                height: 20.w,
              ),
              AppGaps.hGap10,
              Text(
                '产品储存量',
                style: TextStyle(
                  fontSize: 15.sp,
                  color: const Color(0xFF333333),
                  fontWeight: FontWeight.w500,
                ),
              )
            ],
          ),
          AppGaps.vGap18,
          Expanded(
              child: ListView.separated(
                  itemBuilder: (context, index) {
                    return _getCapacityView(controller.productList[index]);
                  },
                  separatorBuilder: (context, index) => AppGaps.vGap16,
                  itemCount: controller.productList.length))
        ],
      ),
    );
  }

  /// 存储量列展示
  Widget _getCapacityView(ProductStorageEntity item) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 14.w),
      decoration: BoxDecoration(
        color: const Color(0xFFF5F6FF),
        borderRadius: BorderRadius.all(
          Radius.circular(6.w),
        ),
      ),
      child: Row(
        children: [
          Expanded(
              child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '仓库名称: ${item.warehouseName}',
                style: TextStyle(
                  fontSize: 15.sp,
                  fontWeight: FontWeight.w500,
                  color: const Color(0xff262F38),
                ),
              ),
              AppGaps.vGap4,
              Text(
                '安全责任人: ${item.sadetyPersonName}',
                style: TextStyle(
                  fontSize: 12.sp,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xffA5A6B5),
                ),
              )
            ],
          )),
          SvgPicture.asset(
            AppImages.icon_product,
            width: 30.w,
            height: 30.w,
            // colorFilter: const ColorFilter.mode(Colors.white, BlendMode.srcIn),
          ),
          Text(
            '${item.total}',
            style: TextStyle(
              fontSize: 16.sp,
              fontWeight: FontWeight.w500,
              color: const Color(0xff495061),
            ),
          ),
          Text(
            '件',
            style: TextStyle(
              fontSize: 12.sp,
              fontWeight: FontWeight.w500,
              color: const Color(0xff495061),
            ),
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            appBar: CustomAppBar(
              title: Text(
                '产品详情',
                style: TextStyle(
                  fontSize: 18.sp,
                  color: Colors.white,
                  fontWeight: FontWeight.w400,
                ),
              ),
              leading: const BackButton(
                color: Colors.white,
              ),
              iconButtonColor: Colors.white,
              backgroundColor: const Color(0xFF5C6DFF),
            ),
            body: Container(
                color: const Color(0xFFF5F5F6),
                child: Stack(
                  children: [
                    // 产品基础信息
                    _getBasicInformation(),
                    Container(
                      margin: EdgeInsets.only(top: 90.w),
                      padding: EdgeInsets.symmetric(horizontal: 16.w),
                      child: Column(
                        children: [
                          // 产品详情
                          _getDetailsView(),
                          AppGaps.vGap14,
                          // 产品储存量
                          Expanded(child: _getStorageCapacityView()),
                          AppGaps.vGap20
                        ],
                      ),
                    )
                  ],
                )),
          );
        });
  }
}
